// pages/trucks/bindInfor/bindInfor.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '', //姓名
    phone: '', //手机号
    code: '', //验证码
    is_code: false, // 是否获取到code
    timer: null,
    is_sendcode: false, // 60s
    time_count: 60,
    positive: '/image/body.png', //默认身份证人像面
    reverses: '/image/national.png', //默认身份证国徽面
    personal: '/image/personal.png', //默认本人照片
    type: '' //审核状态
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
    this.getInfor();
  },
  //姓名
  bindName(e) {
    console.log(e)
    this.setData({
      name: e.detail.value
    })
  },
  //手机号
  bindPhone(e) {
    console.log(e)
    this.setData({
      phone: e.detail.value
    })
  },
  //验证码
  bindCode(e) {
    console.log(e)
    this.setData({
      code: e.detail.value
    })
  },
  getInfor() {
    wx.request({
      url: app.globalData.url + 'api/v1/goods/goodsresources/information',
      method: 'GET',
      data: {
        id: wx.getStorageSync('id')
      },
      success: res => {
        console.log(res)
        if(res.data.code == 200) {
          this.setData({
            type: res.data.data.examine
          })
          console.log(this.data.type)
        }
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  // 60s
  getSendCode() {
    const TIME_COUNT = 60;
    if (!this.data.timer) {
      this.setData({
        time_count: TIME_COUNT,
        is_sendcode: true
      })
      this.data.timer = setInterval(() => {
        if (this.data.time_count > 0 && this.data.time_count <= TIME_COUNT) {
          this.setData({
            time_count: this.data.time_count-1
          })
        } else {
          this.setData({
            is_sendcode: false,
            timer: null
          })
          clearInterval(this.data.timer);
        }
      }, 1000);
    }
  },
  //上传图片
  uploadImg(e){
    let cramer = e.currentTarget.dataset.type;
    wx.chooseImage({
      sizeType: "compressed", //压缩图
      count: 1,
      success: (res) => {
        console.log(res)
        let path = res.tempFilePaths;
        if(cramer == 0){
          this.setData({
            positive: path[0]
          })
        }else if(cramer == 1){
          this.setData({
            reverses: path[0]
          })
        }else if(cramer == 2){
          this.setData({
            personal: path[0]
          })
        }
        wx.uploadFile({
          url: app.globalData.url + 'api/v1/file/files',
          method: 'POST',
          filePath: path[0],
          name: 'file',
          success: res => {
            console.log(res)
            let arr = JSON.parse(res.data)
            if(cramer == 0){
              this.setData({
                positive: arr.data.url
              })
            }else if(cramer == 1){
              this.setData({
                reverses: arr.data.url
              })
            }else if(cramer == 2){
              this.setData({
                personal: arr.data.url
              })
            }
          },
          fail: err => {
            console.log(err)
          }
        })
      }
    })
  },
  //已认证图片点击
  uploadNoImg() {
    wx.showToast({
      title: '修改资料请联系客服',
      icon: 'none',
      mask: true
    })
  },
  //获取资料信息
  getData() {
    wx.request({
      url: app.globalData.url + 'api/v1/goods/goodslogin/goodsdata',
      method: 'GET',
      data: {
        id: wx.getStorageSync('id')
      },
      success: res => {
        console.log(res)
        if(res.data.code == 200) {
          this.setData({
            positive: res.data.data.positive,
            reverses: res.data.data.negative,
            personal: res.data.data.picture,
            name: res.data.data.name,
            phone: res.data.data.phone
          })
        }
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  //提交
  submit() {
    if(this.data.positive == ''){
      wx.showToast({
        title: '请上传身份证人像面',
        icon: 'none',
        mask: true
      })
    }else if(this.data.reverses == ''){
      wx.showToast({
        title: '请上传身份证国徽面',
        icon: 'none',
        mask: true
      })
    }else if(this.data.personal == ''){
      wx.showToast({
        title: '请上传本人照片',
        icon: 'none',
        mask: true
      })
    }else if(this.data.name == ''){
      wx.showToast({
        title: '请输入您的姓名',
        icon: 'none',
        mask: true
      })
    }else if(!/^[\u4e00-\u9fa5]{0,16}$/.test(this.data.name)){
      wx.showToast({
        title: '请正确输入您的姓名',
        icon: 'none',
        mask: true
      })
    }else if(this.data.phone == ''){
      wx.showToast({
        title: '请输入您的号码',
        icon: 'none',
        mask: true
      })
    }else if(!/^1[3456789]\d{9}$/.test(this.data.phone)){
      wx.showToast({
        title: '手机号格式错误',
        icon: 'none',
        mask: true
      })
    }else if(this.data.code == ''){
      wx.showToast({
        title: '请发送验证码',
        icon: 'none',
        mask: true
      })
    }else{
      wx.request({
        url: app.globalData.url + 'api/v1/goods/goodslogin/goodseredit',
        method: 'POST',
        data: {
          id: wx.getStorageSync('id'),
          positive: this.data.positive, //身份证正面
          negative: this.data.reverses, //身份证反面
          picture: this.data.personal, //本人照片
          name: this.data.name, //真实姓名
          phone: this.data.phone, //手机号码
          code: this.data.code, //验证码
        },
        success: res => {
          console.log(res)
          if(res.data.code == 200) {
            wx.showToast({
              title: '编辑成功',
              icon: 'none',
              mask: true
            })
            setTimeout(() => {
              wx.navigateBack()
            }, 500);
          }else{
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              mask: true
            })
          }
        },
        fail: err => {
          console.log(err)
        }
      })
    }
  },
  //发送验证码
  getCode() {
    if(!/^1[3456789]\d{9}$/.test(this.data.phone)){
      wx.showToast({
        title: '手机号格式错误',
        icon: 'none',
        mask: true
      })
    }else{
      wx.request({
        url: app.globalData.url + 'api/v1/goods/goodslogin/goods-code',
        method: 'GET',
        data: {
          phone: this.data.phone
        },
        success: res => {
          console.log(res)
          if(res.data.code == 200){
            this.getSendCode()
            wx.showToast({
              title: '发送成功',
              icon: 'none',
              mask: true
            })
          }else{
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              mask: true
            })
          }
        },
        fail: err => {
          console.log(err)
        }
      })
    }
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})